<?php 

class TaksasiBase{
    protected $base;
    protected $month_distribution;
    protected $taksasi;
    protected $correction_factor;
  
    public function setBase($b){
        $this->base = $b;
    }
    
    public function setMonthDistribution($d){
        $this->month_distribution = $d;
    }
    
    public function setCorrectionFactor($c){
        $this->correction_factor = $c;
    }
    
    public function getTaksasi(){
        if( $this->taksasi == null ){
            $this->calculate();
        }
        
        return $this->taksasi;
    }
     
    public function calculate(){
        // update base
        $base = $this->base;
        if( is_array($this->correction_factor) ){
            foreach($this->correction_factor as $f){
                foreach($base as $tm=>$nJanjang){
                    $this->base[$tm] *= $f;
                }
            }
        }
        else{
            foreach($base as $tm=>$nJanjang){
                $this->base[$tm] *= $this->correction_factor;
            }
        }
        
        // distribution
		$janjangDist = array();

		foreach($this->month_distribution as $month=>$percent){
 
			foreach($this->base as $tm=>$nJanjang){
			
				$janjangDist[$tm][$month] = round($percent*$nJanjang,0);
			}
		}
        
        return $this->accumulate($janjangDist);
    }
    
    private function accumulate($distribution){
        $acc = array();
        foreach($distribution as $tm=>$value){
            foreach($value as $month=>$dist){
                $acc[$month]+=$dist;
            }
        }
        
        return $acc;
    }
}

class TaksasiSaleable{
    protected $base;
    protected $month_distribution;
    protected $taksasi;
    protected $correction_factor;
    protected $bpj;
  
    public function setBase($b){
        $this->base = $b;
    }
    
    public function setBpj($bpj){
        $this->bpj = $bpj;
    }    
    
    public function setMonthDistribution($d){
        $this->month_distribution = $d;
    }
    
    public function setCorrectionFactor($c){
        $this->correction_factor = $c;
    }
    
    public function getTaksasi(){
        if( $this->taksasi == null ){
            $this->calculate();
        }
        
        return $this->taksasi;
    }
     
    public function calculate(){
        // update base
        $base = $this->base;
        if( is_array($this->correction_factor) ){
            foreach($this->correction_factor as $f){
                foreach($base as $tm=>$nJanjang){
                    $this->base[$tm] *= $f;
                }
            }
        }
        else{
            foreach($base as $tm=>$nJanjang){
                $this->base[$tm] *= $this->correction_factor;
            }
        }
        
        // distribution
		$janjangDist = array();

		foreach($this->month_distribution as $month=>$percent){
 
			foreach($this->base as $tm=>$nJanjang){
			
				$janjangDist[$tm][$month] = round($percent*$nJanjang,0);
			}
		}
        
        $bpjJanjang = $this->multiplyBpj($janjangDist);
        
        return $this->accumulate($bpjJanjang);
    }
    
    private function multiplyBpj($janjangDist){
        $bpjResult = array();
        foreach($janjangDist as $tm=>$value){
            
            foreach($value as $month=>$dist){
                $bpjResult[$tm][$month] = $dist*$this->bpj[$tm];
            }
        }
        
        return $bpjResult;
    }
    
    private function accumulate($distribution){
        $acc = array();
        foreach($distribution as $tm=>$value){
            foreach($value as $month=>$dist){
                $acc[$month]+=$dist;
            }
        }
        
        return $acc;
    }
}
?>